package com.sz.app.oss.api.system;

import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import com.sz.common.core.system.entity.SysConfig;
import com.sz.common.core.system.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: SysConfigController <br>
 * Author: Charles <br>
 * Date: 2016-11-22 16:31:00
 */
@Controller
@RequestMapping("/api/v1/sys/configs")
@Api(description = " ", tags = "008、系统配置项")
public class SysConfigController {
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * @param domainId --域Id(1/2/3)
     * @param catalog  --分类
     * @return
     * @throws BizException
     */
    @ApiOperation(value = "获取配置列表", notes = "获取配置项列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "domainId", value = "域Id(1/2/3)", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "catalog", value = "分类", dataType = "string", required = false, paramType = "query"),
    })
    @RequestMapping(value = "/{domainId}", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto list(@PathVariable int domainId,
                          @RequestParam(required = false, defaultValue = "default") String catalog) throws BizException {

        List<SysConfig> list = sysConfigService.getProperties(domainId, catalog);
        ResultDto result = new ResultDto(list);
        return result;
    }

    /**
     * 设置属性值
     *
     * @param domain  --域Id
     * @param catalog --分类
     * @param key     --key
     * @param value   --值
     * @return
     */
    @ApiOperation(value = "设置属性值", notes = "设置属性的值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "domain", value = "域Id(1/2/3)", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "catalog", value = "分类", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "key", value = "key", dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "value", value = "值", dataType = "string", paramType = "body")
    })
    @RequestMapping(value = "/{domain}/{catalog}/{key}", method = {RequestMethod.POST})
    @ResponseBody
    public ResultDto getLogById(@PathVariable int domain,
                                @PathVariable String catalog,
                                @PathVariable String key,
                                @RequestBody String value) {
        sysConfigService.setProperty(domain, catalog, key, value);
        ResultDto resultDto = new ResultDto();
        return resultDto;
    }
}
